<?php
/**
 */
class TriggersRelationsTable extends Doctrine_Table
{
	public function addTriggerRelation($trigger_id,$user_id,$is_owner,$group_id)
	{
		
				
		$TriggersRelations = new TriggersRelations();
		$TriggersRelations ->trigger_id = $trigger_id;
		$TriggersRelations ->user_id = $user_id;
		$TriggersRelations ->is_owner = $is_owner;
		$TriggersRelations ->status = "Active";
		$TriggersRelations ->read_status = '0';
		$TriggersRelations ->group_id = $group_id;
		$TriggersRelations ->owner_id = $_SESSION["sess_userid"];
		//echo '<pre>';print_r($Expressions);exit;
		$TriggersRelations ->save();
		return $TriggersRelations->id;
	}
	public function deleteTrigger($trigger_id,$user_id,$status)
	{
		$query = new Doctrine_Query();
		$query->update('TriggersRelations e')
			  ->set('e.status',"'$status'")
			  ->where("e.trigger_id='".$trigger_id."'")
			  ->andWhere("e.user_id='".$user_id."'");
			   //echo $query;exit;
		$result=$query->execute();
	return $result;		
	}
	public function updateTriggerReadStatus($user_id,$trigger_id,$status)
	{
		$query = new Doctrine_Query();
		$query->update('TriggersRelations e')
			  ->set('e.read_status',"'$status'")
			  ->where("e.trigger_id='".$trigger_id."'")
			  ->andWhere("e.user_id='".$user_id."'");
			   //echo $query;exit;
		$result=$query->execute();
	return $result;		
	}
	public function deleteTriggersByTriggerId($trigger_id)
	{
		$query = new Doctrine_Query();
		$query->update('TriggersRelations e')
			  ->set('e.status',"'Inactive'")
			  ->where("e.trigger_id='".$trigger_id."'");
			     //echo $query;exit;
		$result=$query->execute();
	return $result;		
	}
	public function deleteTriggersByRelationId($relation_id)
	{
		$query = new Doctrine_Query();
		$query->update('TriggersRelations e')
			  ->set('e.status',"'Inactive'")
			  ->where("e.id='".$relation_id."'");
			   //echo $query;exit;
		$result=$query->execute();
	return $result;		
	}
	public function checkTriggerRelation($Trigger_id,$user_id)
	{
		$query = new Doctrine_Query();
		$query->select('count(id) as numCount')
		       ->from('TriggersRelations t')
			   ->where("t.user_id='".$user_id."'")
			   ->andWhere("t.trigger_id='".$Trigger_id."'");
			   //echo $query;exit;
			 $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0];
	        
	}
	public function getOwnerDetailsByTriggerId($trigger_id)
	{
		//echo $user_id;exit;
		$query = new Doctrine_Query();
		$query->select('t.owner_id')
		       ->from('TriggersRelations t')
			   ->where("t.trigger_id='".$trigger_id."'");
			   		  // echo $query;exit;
			   $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result[0];
	        
	}
	public function getUsersDetailsByTriggerId($trigger_id)
	{
		//echo $user_id;exit;
		$query = new Doctrine_Query();
		$query->select('t.user_id')
		       ->from('TriggersRelations t')
			   ->where("t.trigger_id='".$trigger_id."'");
			   		  // echo $query;exit;
			   $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result;
	        
	}
	public function getAllTriggersByUserId($owner_id)
	{
		//echo $user_id;exit;
		$query = new Doctrine_Query();
		$query->select('t.id,t.trigger_id,t.user_id')
		       ->from('TriggersRelations t')
			   ->where("t.owner_id='".$owner_id."'")
			   ->andWhere("t.send_mail='0'")
			   ->andWhere("t.is_owner!='1'");
			   		  // echo $query;exit;
			   $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result;
	        
	}
	public function updateTriggerSendMailStatus($relation_id)
	{
		$query = new Doctrine_Query();
		$query->update('TriggersRelations e')
			  ->set('e.send_mail',"'1'")
			  ->where("e.id='".$relation_id."'");
			   //echo $query;exit;
		$result=$query->execute();
	return $result;		
	}
	
}